@isTest
private class CreateIngenioAccountControllerTest{

    static ID aasMasterClientRT;
    static ID aasAgencyRT ;
    static ID aasSalesAccountRT ;
    static ID nsMasterClientRT ;
        
    static ID cmrGroupRT ;
    static ID cmrAgencyRT ;
    static ID cmrAgencyClientRT ;
    
    static ID aFrontierManagedCustomer;
    static ID ingenioManagedCustomer ;   
    
    public static void Init(){
        aasMasterClientRT = RecordTypeHelper.GetRecordTypeIDByDevName('Account', 'AAS_Master_Client');
        aasAgencyRT = RecordTypeHelper.GetRecordTypeIDByDevName('Account', 'AAS_Agency_Account');
        aasSalesAccountRT = RecordTypeHelper.GetRecordTypeIDByDevName('Account', 'Advanced_Ad_Solutions');

        nsMasterClientRT = RecordTypeHelper.GetRecordTypeIDByDevName('Account', 'NS_Master_Client');
        cmrGroupRT = RecordTypeHelper.GetRecordTypeIDByDevName('Account', 'CMR_Group');        
        cmrAgencyRT = RecordTypeHelper.GetRecordTypeIDByDevName('Account', 'CMR_Agency');        
        cmrAgencyClientRT = RecordTypeHelper.GetRecordTypeIDByDevName('Account', 'CMR_Agency_Client');

        aFrontierManagedCustomer= RecordTypeHelper.GetRecordTypeIDByDevName('Account', 'CMR_Agency_Managed');
        ingenioManagedCustomer = RecordTypeHelper.GetRecordTypeIDByDevName('Account', 'National_Managed_Customer_Ingenio');
    }

    static testMethod void controllerTest() {
		string city = 'Springfield';
		string lastname = 'Test';
		
		Init();
		    	
		//User u = CreateUser(Role_OV);
		//System.runAs(u){

		ID masterClientId;
		ID agencyId;
		ID agencyClientId;
		
		// Create a Master Client
        Account a = new Account();
        a.RecordTypeId = aasMasterClientRT ;
        a.Name = 'Test AAS Master Client';
        a.Account_Status__c = 'Active';
        insert a;
        masterClientId = a.ID;
        
        // Create an AAS Agency
        a = new Account();
        a.RecordTypeId = aasAgencyRT ;
        a.Name = 'Test Aas Agency';
        a.Account_Status__c = 'Active';
        insert a;
        agencyId = a.ID;
        
        a = new Account();
        a.RecordTypeId = aasSalesAccountRT;
        a.Name = 'Test AAS Sales Account';
        a.AAS_Agency_Name__c = agencyId;        
        a.Master_Client__c = masterClientId;
        a.Account_Status__c = 'Active';
        a.BillingCity = city;
        insert a;
        agencyClientId = a.ID;
        
        Contact c = new Contact();
        c.FirstName = 'Unit';
        c.LastName = lastname;
        c.AccountId = agencyClientId;
        insert c;
         
        Opportunity o = new Opportunity();
        o.Name = 'Test Opportunity';
        o.RecordTypeId = RecordTypeHelper.GetRecordTypeIDByDevName('Opportunity', 'Advanced_Ad_Solutions');
        o.AccountId = a.ID;
        o.Credit_Check_Info_Secured__c = 'Yes';
        o.Contract_Start_Date__c = Date.today().addDays(1);
        o.Contract_Termination_Date__c = Date.today().addDays(20);
        o.StageName = 'Ready to Close';
        o.Probability = 90;
        o.Amount = 2000;
        o.CloseDate = Date.today().addDays(10);
        insert o; 
			    
					    
        //Use the PageReference Apex class to instantiate a page
        PageReference pageRef = Page.CreateIngenioAccount;
        Test.setCurrentPage(pageRef);
        ApexPages.currentPage().getParameters().put('ID', o.ID);
        
        ApexPages.Standardcontroller sc = new ApexPages.Standardcontroller(o);
        CreateIngenioAccountController controller = new CreateIngenioAccountController(sc);
    
		Test.startTest();
		controller.CreateAccount();
		Test.stopTest();
		
		System.assertEquals(null, controller.ErrorMessage, 'Did not expect an error message');
		List<Account> ingenioAccounts = [SELECT ID, BillingCity from Account where ParentID =:agencyClientId and RecordTypeID =:ingenioManagedCustomer];
		System.assertEquals(1, ingenioAccounts.size(), 'Expecing one newly created Ingenio Account linked to the AAS Agency Client');
		System.assertEquals(city, ingenioAccounts[0].BillingCity, 'Expecing the city to be copied from the Agency Client');		

		List<Contact> ingenioContacts = [SELECT ID, FirstName, LastName from Contact where AccountID =:ingenioAccounts[0].ID];
		System.assertEquals(1, ingenioAccounts.size(), 'Expecing one newly created Contact linked to the Ingenio Managed Account');
		System.assertEquals(lastname, ingenioContacts[0].LastName, 'Expecing the last name to be copied from the Agency Clients contact');		
		
    }

}